package com.xiaoyu.lanling.app

import `in`.srain.cube.concurrent.AppThreads
import `in`.srain.cube.util.UTUtil
import `in`.srain.cube.util.log.LogEvent
import android.content.Context
import com.alibaba.security.realidentity.RPVerify
import com.ishumei.smantifraud.SmAntiFraud
import com.ishumei.smantifraud.SmAntiFraud.SmOption
import com.xiaoyu.base.AppConfig
import com.xiaoyu.base.log.AppPerformanceLogClient
import com.xiaoyu.base.utils.MMKVUtil.Companion.getDefault
import com.xiaoyu.lanling.common.Constant

class PrivacySDKInitProcessor {

    companion object {

        @JvmStatic
        fun initSDKIfAgreePrivacyAsync(context: Context) {
            AppThreads.runOnInitThread {
                val agree = getDefault().getBoolean(Constant.MMKVKey.KEY_USER_AND_PRIVACY_PROTOCOL_AGREE, false)
                if (!agree) {
                    return@runOnInitThread
                }
                val logEvent = LogEvent("init-privacy-async")
                logEvent.startTimeForKey("total-time")
                logEvent.startTimeForKey("init-sm-anti-fraud");
                initSmAntiFraud(context)
                logEvent.endTimeForKey("init-sm-anti-fraud");

                logEvent.startTimeForKey("init-ali-rp");
                initAliRp(context)
                logEvent.endTimeForKey("init-ali-rp");

                logEvent.endTimeForKey("total-time")
                AppPerformanceLogClient.instance.addLog(logEvent)
            }
        }

        private fun initSmAntiFraud(context: Context) {
            val option = SmOption()
            option.organization = AppConfig.shumeiOrganization()
            option.appId = "yizhoucp" // use the same app id
            option.channel = UTUtil.getInstance().channelId
            SmAntiFraud.create(context, option)
        }

        private fun initAliRp(context: Context) {
            RPVerify.init(context)
        }
    }
}